package com.fz.controller;

import com.fz.mapper.BookMapper;
import com.fz.mybatis.MyHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by webrx on 2017-09-04.
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @RequestMapping("/show")
    public String show(Model model){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        BookMapper bdao = ss.getMapper(BookMapper.class);
        model.addAttribute("books",bdao.queryAll());
        return "book";
    }

    @RequestMapping("/del/{id}")
    public String del(@PathVariable("id") int id){
        MyHelper mh = new MyHelper();
        SqlSession ss = mh.getSession();
        BookMapper bdao = ss.getMapper(BookMapper.class);
        bdao.deleteById(id);
        ss.commit();
        return "redirect:/book/show";  //重定向，
    }
}
